// 头像裁剪上传
<template>
  <!-- 引入插件  npm install vue-cropper --save -->
  <div>
    <el-dialog title="修改头像" :visible.sync="editAvatarDialog" width="600px">
      <el-row type="flex" justify="center">
        <div class="cropper">
          <vueCropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.size"
            :outputType="option.outputType"
            :info="option.info"
            :full="option.full"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :original="option.original"
            :autoCrop="option.autoCrop"
            :autoCropWidth="option.autoCropWidth"
            :autoCropHeight="option.autoCropHeight"
            :fixedBox="option.fixedBox"
            @realTime="realTime"
          ></vueCropper>
        </div>
        <div class="previewBox">
          <div :style="previews.div" class="preview">
            <img :src="previews.url" :style="previews.img" />
          </div>
          <el-row type="flex" justify="center">
            <el-upload
              action=""
              :show-file-list="false"
              :auto-upload="false"
              :on-change="uploadImg"
            >
              <el-button size="mini" type="primary"> 更换头像 </el-button>
            </el-upload>
          </el-row>
          <br />
          <el-row class="btn-row">
            <el-button
              icon="el-icon-plus"
              circle
              size="mini"
              @click="changeScale(1)"
            ></el-button>
            <el-button
              icon="el-icon-minus"
              circle
              size="mini"
              @click="changeScale(-1)"
            ></el-button>
            <el-button
              icon="el-icon-download"
              circle
              size="mini"
              @click="down('blob')"
            ></el-button>
            <el-button
              icon="el-icon-refresh-left"
              circle
              size="mini"
              @click="rotateLeft"
            ></el-button>
            <el-button
              icon="el-icon-refresh-right"
              circle
              size="mini"
              @click="rotateRight"
            ></el-button>
          </el-row>
        </div>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAvatarDialog = false">取 消</el-button>
        <el-button type="primary" @click="saveEditAvatar">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
// chilDone 保存按钮后按下后的事件

<script>
import { VueCropper } from "vue-cropper";
export default {
  name: "avatarUpload",
  components: { VueCropper },
  data() {
    return {
      previews: {},
      option: {
        img: "", // 裁剪图片的地址
        info: true, // 裁剪框的大小信息
        outputSize: 1, // 剪切后的图片质量（0.1-1）
        full: true, // 输出原图比例截图 props名full
        outputType: "png", // 裁剪生成额图片的格式
        canMove: true, // 能否拖动图片
        original: false, // 上传图片是否显示原始宽高
        canMoveBox: true, // 能否拖动截图框
        autoCrop: true, // 是否默认生成截图框
        autoCropWidth: 150,
        autoCropHeight: 150,
        fixedBox: true, // 截图框固定大小
      },
    };
  },

  props: {
    // 控制弹窗 需要双向绑定
    dialogVisible: {
      type: Boolean,
      default: () => false,
    },
    // 裁剪的url 需要双向绑定
    clipImg: {
      type: String,
      default: () => "",
    },
    upType: {
      type: String,
      default: () => "blob",
    },
  },
  computed: {
    editAvatarDialog: {
      get() {
        return this.dialogVisible;
      },
      set(newVal) {
        this.$emit("update:dialogVisible", newVal);
      },
    },
    avatarURL: {
      get() {
        return this.clipImg;
      },
      set(newVal) {
        this.$emit("update:clipImg", newVal);
      },
    },
  },

  methods: {
    //   弹窗打开
    editAvatar() {
      this.editAvatarDialog = true;
      this.option.img = this.avatarURL;
    },
    // 保存头像修改
    saveEditAvatar() {
      this.finish(this.upType);
    },
    // 放大/缩小
    changeScale(num) {
      num = num || 1;
      this.$refs.cropper.changeScale(num);
    },
    // 左旋转
    rotateLeft() {
      this.$refs.cropper.rotateLeft();
    },
    // 右旋转
    rotateRight() {
      this.$refs.cropper.rotateRight();
    },
    // 保存上传图片
    finish(type) {
      if (type === "blob") {
        let blobData;
        new Promise((reslove, reject) => {
          this.$refs.cropper.getCropBlob((data) => {
            blobData = window.URL.createObjectURL(data);
            //弹出数据
            reslove();
          });
        }).then(() => {
          this.$emit("chilDone", blobData);
        });
      } else {
        this.$refs.cropper.getCropData((data) => {
          //访问接口保存到数据库写这儿!
          this.$emit("chilDone", this.dataURLtoFile(data));
        });
      }
    },
    // 实时预览函数
    realTime(data) {
      this.previews = data;
    },
    // 下载图片
    down(type) {
      var aLink = document.createElement("a");
      aLink.download = "author-img";
      if (type === "blob") {
        this.$refs.cropper.getCropBlob((data) => {
          aLink.href = window.URL.createObjectURL(data);
          aLink.click();
        });
      } else {
        this.$refs.cropper.getCropData((data) => {
          aLink.href = data;
          aLink.click();
        });
      }
    },
    // 更换头像--上传本地图片
    uploadImg(file) {
      var _this = this;
      var reader = new FileReader();
      reader.onload = (e) => {
        let data;
        if (typeof e.target.result === "object") {
          // 把Array Buffer转化为blob 如果是base64不需要
          data = window.URL.createObjectURL(new Blob([e.target.result]));
        } else {
          data = e.target.result;
        }
        _this.option.img = data;
      };
      // 转化为base64
      // reader.readAsDataURL(file.raw)
      // 转化为blob
      reader.readAsArrayBuffer(file.raw);
    },
    dataURLtoFile(dataUrl, fileName) {
      var arr = dataUrl.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], fileName, { type: mime });
    },
  },
};
</script>

<style lang="scss" scoped>
.previewBox {
  text-align: center;
  margin-left: 60px;
}

.preview {
  width: 150px;
  height: 150px;
  margin: 0px auto 20px auto;
  border-radius: 50%;
  border: 1px solid #ccc;
  background-color: #ccc;
  overflow: hidden;
}

.cropper {
  width: 260px;
  height: 260px;
}

.btn-row {
  .el-button {
    line-height: 13px;
  }
}
</style>

// 例子
    <AvatarUpload
      :dialogVisible.sync="avatarUploadVisible"
      @chilDone="chilDone"
      :upType="null"
    ></AvatarUpload>

    chilDone(file){
      // file 就是文件
    }